package com.seadragon.app.dms.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.seadragon.app.dms.model.Customer;
import com.seadragon.app.dms.model.DmsConstants;

public class AccessControlInterceptor extends HandlerInterceptorAdapter {

	private String userType;//customer or driver
	private String redirectUrl;
	
	public void setUserType(String userType) {
		this.userType = userType;
	}

	public void setRedirectUrl(String redirectUrl) {
		this.redirectUrl = redirectUrl;
	}

	@Override
	public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler) throws Exception {
		Customer user = (Customer)request.getSession().getAttribute(DmsConstants.SESSION_SCOPED_USER);
		if (user!=null/*&&user.getType().equals(this.userType)*/){//user logged in and user type matches
			return true;
		} else {
			response.sendRedirect(this.redirectUrl);
            return false;			
		}
    }

}
